<template>
    <div class="edge-tab-container">
        <!-- 左侧按钮区域 -->
        <div class="left-sidebar">
            <div class="sidebar-buttons">
                <button class="sidebar-btn active" @click="switchTab('lua', $event)">
                    <div class="btn-icon">📝</div>
                    <div class="btn-text">Lua代码编辑</div>
                </button>
                <button class="sidebar-btn" @click="switchTab('mqtt', $event)">
                    <div class="btn-icon">📡</div>
                    <div class="btn-text">MQTT消息格式</div>
                </button>
                <button class="sidebar-btn" @click="switchTab('graph', $event)">
                    <div class="btn-icon">🔗</div>
                    <div class="btn-text">图形编程</div>
                </button>
            </div>
        </div>

        <!-- 右侧内容区域 -->
        <div class="content-area" :class="{ 'no-padding': currentTab === 'lua' || currentTab === 'mqtt' }">
            <div v-if="currentTab === 'lua'" class="tab-content lua-editor-tab">
                <LuaEditor ref="luaEditor" />
            </div>
            <div v-else-if="currentTab === 'mqtt'" class="tab-content mqtt-editor-tab">
                <MqttSchemaEditor ref="mqttEditor" />
            </div>
            <div v-else-if="currentTab === 'graph'" class="tab-content">
                <p>此功能正在开发中...</p>
            </div>
        </div>
    </div>
</template>
<script>
import MqttSchemaEditor from '@/components/EdgeTab/MqttFormatJson/MqttSchemaEditor.vue'
import LuaEditor from '@/components/EdgeTab/LuaEdit/LuaEditor.vue'
import { mapGetters } from 'vuex'

export default {
    name: 'EdgeTab',
    components: {
        MqttSchemaEditor,
        LuaEditor
    },
    data() {
        return {
            currentTab: 'lua'
        }
    },

    computed: {
        ...mapGetters('groupTree', ['selectedDeviceId', 'selectedGroupId', 'selectedDeviceInfo', 'selectedGroupInfo'])
    },

    watch: {
        // 监听选中设备ID的变化
        selectedDeviceId: {
            handler(newDeviceId, oldDeviceId) {
                console.log('🔍 [EdgeTab] 选中设备ID发生变化:', {
                    old: oldDeviceId,
                    new: newDeviceId
                })
                // 当选中设备发生变化时
                if (newDeviceId !== oldDeviceId) {
                    if (newDeviceId && newDeviceId !== oldDeviceId) {
                        // 通知子组件重新加载数据
                        this.$nextTick(() => {
                            const mqttEditor = this.$refs.mqttEditor
                            const luaEditor = this.$refs.luaEditor
                            if (mqttEditor) mqttEditor.reloadData()
                            if (luaEditor) luaEditor.reloadData()
                        })
                    }
                }
            },
            immediate: true
        }
    },

    methods: {
        switchTab(tab, event) {
            this.currentTab = tab
            // 更新按钮的active状态
            document.querySelectorAll('.sidebar-btn').forEach(btn => {
                btn.classList.remove('active')
            })
            event.currentTarget.classList.add('active')
        }
    }
}
</script>
<style scoped>
.edge-tab-container {
    display: flex;
    height: 100%;
    background-color: #f5f5f5;
}
.left-sidebar {
    width: 200px;
    background-color: #fff;
    border-right: 1px solid #e0e0e0;
    padding: 20px 0;
    box-shadow: 2px 0 4px rgba(0, 0, 0, 0.1);
}
.sidebar-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 0 15px;
}
.sidebar-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 15px 10px;
    border: none;
    background-color: transparent;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    width: 100%;
    text-align: center;
}
.sidebar-btn:hover {
    background-color: #f0f8ff;
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.sidebar-btn.active {
    background-color: #1890ff;
    color: white;
    box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}
.btn-icon {
    font-size: 24px;
    margin-bottom: 8px;
}
.btn-text {
    font-size: 14px;
    font-weight: 500;
    line-height: 1.2;
}
.content-area {
    flex: 1;
    padding: 30px;
    overflow: hidden; /* 改为hidden，避免滚动条干扰 */
    display: flex;
    flex-direction: column;
}
.content-area.no-padding {
    padding: 0; /* Lua编辑器标签页去掉padding，使其与左侧栏高度一致 */
}
.tab-content {
    background-color: #fff;
    border-radius: 8px;
    padding: 30px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.tab-content h2 {
    margin-top: 0;
    margin-bottom: 20px;
    color: #333;
    font-size: 24px;
    font-weight: 600;
}
.tab-content p {
    color: #666;
    font-size: 16px;
    line-height: 1.6;
    margin: 0;
}
.lua-editor-tab,
.mqtt-editor-tab {
    padding: 0 !important; /* 覆盖.tab-content的padding */
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden; /* 防止内容溢出 */
    border-radius: 0; /* 去掉圆角，使其与左侧栏对齐 */
    box-shadow: none; /* 去掉阴影 */
}
</style> 